package net.learn2develop.GPS;

import java.util.List;


import net.learn2develop.DB.DatabaseHelper;
import net.learn2develop.view.MyItemizedOverlay;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;


import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class PlaceInMap extends MapActivity {


    protected int placeId; 
    private MapView mapView;
	private MapController mc;
	private GeoPoint p;
	private String attributes [] = {"nameplace" , "latitud", "longitud", "address", "phone", "url", "email", "icon", "photo", "promotion", "_id"};

	
	class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView,
        boolean shadow, long when)
        {
            super.draw(canvas, mapView, shadow);

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.blue);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
            return true;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_search);
        
        placeId = getIntent().getIntExtra("PLACE_ID", 0);
        SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT nameplace, address, latitud, longitud, phone, url, email, icon, photo, promotion, _id FROM places WHERE _id LIKE ?", 
				new String[]{""+placeId});
        
        if (cursor.moveToFirst())
        {
        	do{
        
        	mapView = (MapView) findViewById(R.id.mapView);
		    mc = mapView.getController();

	        double lat = Double.parseDouble(cursor.getString(cursor.getColumnIndex("latitud")));
	        double lon = Double.parseDouble(cursor.getString(cursor.getColumnIndex("longitud")));

	        p = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
	        mc.animateTo(p);
	        mc.setZoom(15);


	        //---Add a location marker---
	        List<Overlay> listOfOverlays = mapView.getOverlays();
	        listOfOverlays.clear();
	        
	        //--Add a Balloon Map in marker--
	        String image = "<img src='"+cursor.getString(cursor.getColumnIndex("photo"))+"'>"; 
	       	MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(this.getResources().getDrawable(R.drawable.blue), mapView, image, 
	       										Integer.valueOf(cursor.getString(cursor.getColumnIndex("_id"))));
			String info = " ";
	    	for (int r = 3;r<7;r++){
				info = info+cursor.getString(cursor.getColumnIndex(attributes[r]))+" ";
			} 	
			OverlayItem overlayItem = new OverlayItem(p, cursor.getString(cursor.getColumnIndex("nameplace")),info);
			itemizedOverlay.addOverlay(overlayItem);
			listOfOverlays.add(itemizedOverlay);
	        mapView.invalidate();
	        
        	} while (cursor.moveToNext());
		}
		
		if (cursor != null && !cursor.isClosed()) {
	         cursor.close();
		}
 
    }
    
    

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
    
}